package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.SkuEsMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SkuService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 1
 * @data 2022/1/16
 */
@Service
public class SkuServiceImpl implements SkuService {

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SkuEsMapper skuEsMapper;

    @Override
    public void importSku() {

        //调用feign中的方法获取查询到的数据
        Result<List<Sku>> byStatus = skuFeign.findByStatus("1");
        //取出Result中的数据sku集合
        List<Sku> skuList = byStatus.getData();
        //进行非空判断
        if (skuList != null && skuList.size() > 0) {
            //转换将List<sku>转换为List<skuinfo>
            List<SkuInfo> skuInfos = JSON.parseArray(JSON.toJSONString(skuList),SkuInfo.class);
            for (SkuInfo skuInfo : skuInfos) {
                String spec = skuInfo.getSpec();//{"电视音响效果":"环绕","电视屏幕尺寸":"20英寸","尺码":"165"}
                skuInfo.setSpecMap(JSON.parseObject(spec, Map.class));
            }
            skuEsMapper.saveAll(skuInfos);
        }
    }
}
